package com.ciaojian.core.bytedance.api;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ciaojian.core.bytedance.MiniAppRequestApi;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.Map;

/**
 * 抖音请求
 */
public class ByteDanceRequestApi {

    public static String sendRequest(MiniAppRequestApi miniAppRequestApi, Map<String, Object> param) {
        if (miniAppRequestApi.getRequestType().equals("POST")){
            return post(miniAppRequestApi, param);
        }
        if (miniAppRequestApi.getRequestType().equals("HTTPPOST")||miniAppRequestApi.getRequestType().equals("HTTPGET")){
            return httpPostAndGet(miniAppRequestApi, param);
        }
        return null;
    }

    public static String httpPostAndGet(MiniAppRequestApi miniAppRequestApi, Map<String, Object> param){
        CloseableHttpClient client = HttpClients.createDefault();
        StringEntity entity = new StringEntity(param.toString(), "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");

        if (miniAppRequestApi.getRequestType().equals("HTTPPOST")){
            HttpPost httpPost = new HttpPost(miniAppRequestApi.getUrl());
            httpPost.setEntity(entity);
            return httpPost(client, httpPost);
        }
        if (miniAppRequestApi.getRequestType().equals("HTTPGET")){
            return null;
        }
        return null;
    }




    public static String post(MiniAppRequestApi miniAppRequestApi, Map<String, Object> param) {
        return HttpRequest.post(miniAppRequestApi.getUrl()).contentType("multipart/form-data").form(param).execute().body();
    }

    private static String httpPost(CloseableHttpClient client, HttpPost httpPost) {
        try {
            HttpResponse response = client.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
            return null;
        }catch (Exception e){
            return null;}
    }

    private static String httpGet(CloseableHttpClient client, HttpGet httpGet) {
        try {
            HttpResponse response = client.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
            return null;
        }catch (Exception e){
            return null;}
    }

    public static String sendRequestClienttoken(MiniAppRequestApi miniAppRequestApi, String bytedance_clienttoken,String str, JSONObject param) {
        CloseableHttpClient client = HttpClients.createDefault();
        StringEntity entity = new StringEntity(param.toString(), "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        if (miniAppRequestApi.getRequestType().equals("HTTPPOST")){
            HttpPost httpPost = new HttpPost(miniAppRequestApi.getUrl()+bytedance_clienttoken);
            httpPost.setEntity(entity);
            return httpPost(client, httpPost);
        }
        if (miniAppRequestApi.getRequestType().equals("HTTPGET")){
            HttpGet httpGet = new HttpGet(miniAppRequestApi.getUrl()+str+"&access_token="+bytedance_clienttoken);
            return httpGet(client,httpGet);
        }
        return null;
    }
}
